package com.mooltik;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.*;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import com.mooltik.content.Notes;


public class NotesActivity extends Activity implements AdapterView.OnItemClickListener, View.OnClickListener {

    private ListView listView;

    private static final int CONTEXT_MENU_DELETE_ITEM = 0;
    private static final String TAG = "NotesActivity";

    private static final int EDIT_CODE = 0;
    private static final int INSERT_CODE = 1;
    private static final int SYNC_CODE = 2;

    private Cursor cursor;
    private ContentResolver contentProvider;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);
        createListView();

    }


    private void createListView() {

        int rowLayout = R.layout.row;

        contentProvider = getContentResolver();
        cursor = contentProvider.query(Constants.CONTENT_TYPE, null, null, null, null);

        SimpleCursorAdapter notesAdaptor = new SimpleCursorAdapter(this, rowLayout, cursor,
                new String[]{Notes.TITLE, Notes.NOTE}, new int[]{R.id.title, R.id.text});

        listView = (ListView) findViewById(android.R.id.list);
        listView.setAdapter(notesAdaptor);
        listView.setOnCreateContextMenuListener(this);
        listView.setOnItemClickListener(this);
    }


    public void onClick(View view) {

        // create new note
        if (view.getId() == R.id.addNewNoteButton) {

            Intent intent = new Intent(this, EditorActivity.class);

            String notesCount = String.valueOf(cursor.getCount() + 1);
            intent.putExtra(Notes.TITLE, getString(R.string.note) + " " + notesCount);
            intent.putExtra(Notes.NOTE, "");

            startActivityForResult(intent, INSERT_CODE);
        }
    }


    private void sync() {

    }


    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        if (requestCode == INSERT_CODE && resultCode == RESULT_OK) {

            ContentValues values = new ContentValues();
            values.put(Notes.TITLE, intent.getStringExtra(Notes.TITLE));
            values.put(Notes.NOTE, intent.getStringExtra(Notes.NOTE));

            contentProvider.insert(Constants.CONTENT_TYPE, values);

        } else if (requestCode == EDIT_CODE && resultCode == RESULT_OK) {

            ContentValues values = new ContentValues();
            String id = intent.getStringExtra(Notes._ID);
            values.put(Notes.TITLE, intent.getStringExtra(Notes.TITLE));
            values.put(Notes.NOTE, intent.getStringExtra(Notes.NOTE));

            contentProvider.update(Constants.CONTENT_TYPE, values, Notes._ID + " = " + id, null);


        } else if (requestCode == SYNC_CODE && resultCode == RESULT_OK) {


        }
        cursor.requery();

    }


    public void onItemClick(AdapterView<?> adapterView, View view, int idx, long l) {

        Intent intent = new Intent(this, EditorActivity.class);

        Cursor cursor = (Cursor) listView.getItemAtPosition(idx);

        intent.putExtra(Notes._ID, cursor.getString(cursor.getColumnIndex(Notes._ID)));
        intent.putExtra(Notes.TITLE, cursor.getString(cursor.getColumnIndex(Notes.TITLE)));
        intent.putExtra(Notes.NOTE, cursor.getString(cursor.getColumnIndex(Notes.NOTE)));

        startActivityForResult(intent, EDIT_CODE);
    }


    @Override
    public void onCreateContextMenu(ContextMenu contextMenu, View v, ContextMenu.ContextMenuInfo menuInfo) {

//        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
        contextMenu.add(0, CONTEXT_MENU_DELETE_ITEM, 1, R.string.delete);
    }


    @Override
    public boolean onContextItemSelected(MenuItem item) {

        switch (item.getItemId()) {
            case CONTEXT_MENU_DELETE_ITEM:

                AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo();
                Long id = listView.getItemIdAtPosition(info.position);
                contentProvider.delete(Constants.CONTENT_TYPE, Notes._ID + " =  " + id, null);
                cursor.requery();

                return true;
        }

        return false;
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.sync:
                sync();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }


    @Override
    protected void onStop() {
        super.onStop();


    }
}


